<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
    <script>
      // var i
      // let allLi = document.querySelector('li')
      // for (i = 0; i <= allLi.length; i++) {
      //   allLi[i].addEvenListener('click', function () {
      //     console.log(i)
      //     alert(allLi[i].innerHTML)
      //   })
      // }
      var i
      let allLi = document.querySelectorAll('li')

      for (i = 0; i < allLi.length; i++) {
        ;(function (i) {
          allLi[i].addEventListener('click', function () {
            console.log(i)
            alert(allLi[i].innerHTML)
          })
        })(i)
      }
    </script>
  </body>
</html>
